<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/5/22
 * Time: 15:47
 */
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Lib\upload;
use App\Models\Activity;
use App\Models\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class BannerController extends Controller{
    //轮播列表
    public function bannerList(){
        try{
            $banner = Banner::orderBy('id','desc')->get();
            return self::jsondata(0,$banner);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //上传轮播图片
    public function bannerAddApi(Request $request){
        try{
            $files = upload::uploadOne('files');
            $title = $request->input("title",false);
            $type = $request->input("type",false);
            $htmlUrl = $request->input("html_url",false);
            if (!$title || !$type || !$htmlUrl)return self::error(1000);
            $banner = new Banner();
            $banner->title = $title;
            $banner->type = $type;
            $banner->html_url = $htmlUrl;
            $banner->image = $files['data'];
            $banner->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //编辑轮播
    public function bannerEditApi(Request $request){
        try{
            $id = $request->input("id",false);
            $title = $request->input("title");
            $type = $request->input("type");
            $htmlUrl = $request->input("html_url");
            if (!$id)return self::error(1000);
            $banner = Banner::find($id);
            $banner->title = $title;
            $banner->type = $type;
            $banner->html_url = $htmlUrl;
            if ($request->hasFile('files')){
                $files = upload::uploadOne('files');
                $banner->image = $files['data'];
            }
            $banner->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除轮播
    public function bannerDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(100);
            $banner = Banner::find($id);
            $banner->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //活动列表接口
    public function activityListApi(){
        try{
            $activity = Activity::orderBy("id",'desc')->get();
            return self::jsondata(0,$activity);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //上传活动
    public function activityAddApi(Request $request){
        try{
            $files = upload::uploadOne('files');
            $location = $request->input("location",false);
            if (!$location)return self::error(1000);
            $obj = new Activity();
            $obj->location = $location;
            $obj->image = $files['data'];
            $obj->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除活动
    public function activityDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $activity = Activity::find($id);
            $activity->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //编辑活动
    public function activityEditApi(Request $request){
        try{
            $id = $request->input("id",false);
            $location = $request->input("location");
            if (!$id)return self::error(1000);
            $banner = Activity::find($id);
            $banner->location = $location;
            if ($request->hasFile('files')){
                $files = upload::uploadOne('files');
                $banner->image = $files['data'];
            }
            $banner->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
}